package com.kogi.androidpatt.dealing.dynamiclist;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import com.kogi.androidpatt.dealing.FileLoader;
import com.kogi.patterns.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;

public class DynamicLoadingList extends Activity implements OnScrollListener{

	EfficientAdapter adapter;// = new Aleph0();
	ListView list;
	//ProgressBar progress;
	int count=0;
	int tam,parte;
	ViewStub stub;
	LinearLayout load;
	public final static int ONE_SECOND = 1000;
	
	final Handler mHan = new Handler();
	
	protected void hilo(){
		Thread th = new Thread(){
			public void run(){
				mHan.postDelayed(ExeAction, 2000);
			}
		};
		th.start();
	}
	
	final Runnable ExeAction = new Runnable(){
		public void run(){
			
			Newrows();
			stub.setVisibility(View.GONE);
		}
	};
	 
	@Override
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.dynamic_loading_list);
	       
	        list = (ListView) findViewById(R.id.listViewDynamicList);
	       
	       	       
	        stub = (ViewStub) findViewById(R.id.stubDynamic);
	        stub.inflate();
	        stub.setVisibility(View.GONE);
	        
	        Context c=this;
	        adapter = new EfficientAdapter(c);
	        list.setAdapter(adapter);
	        
	        list.setOnScrollListener(this);
	        
	    }
	
	public void Newrows(){
		adapter.moreItems();
		adapter.notifyDataSetChanged();
		stub.setVisibility(View.GONE);
		
	}

	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		  boolean loadMore = /* maybe add a padding */
	            firstVisibleItem + visibleItemCount >= totalItemCount;

	        if(loadMore) {
	        	count = 0;
	        	if(adapter.size()!=1){
	        		if(count==0){
	        			stub.setVisibility(View.VISIBLE);	        		
	        			mHan.postDelayed(ExeAction, 2000);
	        			count=1;
	        		}
	        		//hilo();
	            }
	        	else{
	        		AlertDialog.Builder builder = new AlertDialog.Builder(this);
              	  builder.setMessage("No more Data to Load")
              	         .setCancelable(false)
              	         
              	         .setNegativeButton("ok", new DialogInterface.OnClickListener() {
              	             public void onClick(DialogInterface dialog, int id) {
              	                  dialog.cancel();
              	             }
              	         });
              	  AlertDialog alert = builder.create();
              	  alert.show();
	        	}
		      	
	            
	        }
	           

	}


	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
//*************************************adaptador*************************************	
	 public class EfficientAdapter extends BaseAdapter  {

			private Context MyContext;
			private FileLoader data;
			int tam;
			ArrayList<String> Names; 
			ArrayList<String> Flags;
			int finish=0;
			int partes=20;
			
			public EfficientAdapter(Context a) {
				MyContext=a;
				data = new FileLoader(a);
				data.readFiles(23);
				Names = data.getNames();
				Flags = data.getFlags();
				tam= Names.size();
			}
			
			public int size(){
				return finish;
			}
			
			public int moreItems(){
				if(tam-partes>20){
					partes+=20;
				}
				else if(partes==tam){
					finish=1;
					Log.v("Fin Lista","Se termino la lista");
				}
				else if(partes+20>tam){
					partes+=tam-partes;
				}
				return partes;
				
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return partes;
			}

			@Override
			public Object getItem(int arg0) {
				// TODO Auto-generated method stub
				return Names.get(arg0);
			}

			@Override
			public long getItemId(int arg0) {
				// TODO Auto-generated method stub
				return arg0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View grid;
			    
		 	   if(convertView==null){
		 	    grid = new View(MyContext);
		 	    LayoutInflater inflater=getLayoutInflater();
		 	    grid=inflater.inflate(R.layout.custom_row_list_view, parent, false);
		 	    
		 	   }
		 	   else{
		 	    grid = (View)convertView;
		 	   }
		 	   
		 	   ImageView imageView = (ImageView)grid.findViewById(R.id.imageViewListView);
		 	   TextView textView = (TextView)grid.findViewById(R.id.textdealingDataListView);
		 	   Integer tmp=getResources().getIdentifier(Flags.get(position) , "drawable", getPackageName());
		 	   imageView.setImageResource(tmp);
		 	   textView.setText(Names.get(position));

		 	   return grid;
			}

			

		}


}
